package com.zjlanyun.erp.utils;

import android.content.Context;

import com.snappydb.DB;
import com.snappydb.DBFactory;
import com.snappydb.SnappydbException;
import com.zjlanyun.erp.MainApplication;

/**
 * @author: shun
 * @date: 2016-10-11 8:26
 * @Desc: SnappyDB操作类
 */

public class SDBHelper {

    public static void put(String key, Object object, String dbname) {
        try {
            DB snappydb = DBFactory.open(MainApplication.getContext(), dbname);
            snappydb.put(key, object);
            snappydb.close();
        } catch (SnappydbException e) {
            e.printStackTrace();
        }
    }

    public static <T> T getObject(String key, Class<T> className, String dbname){
        T result = null;
        try {
            DB snappydb = DBFactory.open(MainApplication.getContext(), dbname);
            if (snappydb.exists(key)){
                result = snappydb.getObject(key, className);
            }
            snappydb.close();
        } catch (SnappydbException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void put(String key, String val, String dbname) {
        try {
            DB snappydb = DBFactory.open(MainApplication.getContext(), dbname);
            snappydb.put(key, val);
            snappydb.close();
        } catch (SnappydbException e) {
            e.printStackTrace();
        }
    }

    public static String get(String key, String dbname){
        String result = "";
        try {
            DB snappydb = DBFactory.open(MainApplication.getContext(), dbname);
            if (snappydb.exists(key)){
                result = snappydb.get(key);
            }
            snappydb.close();
        } catch (SnappydbException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void putInt(String key, int val, String dbname) {
        try {
            DB snappydb = DBFactory.open(MainApplication.getContext(), dbname);
            snappydb.putInt(key, val);
            snappydb.close();
        } catch (SnappydbException e) {
            e.printStackTrace();
        }
    }

    public static int getInt(String key, int defValue, String dbname){
        int result = defValue;
        try {
            DB snappydb = DBFactory.open(MainApplication.getContext(), dbname);
            if (snappydb.exists(key)){
                result = snappydb.getInt(key);
            }
            snappydb.close();
        } catch (SnappydbException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static boolean checkKeyExist(String key, String dbname){
        boolean result = false;
        try {
            DB snappydb = DBFactory.open(MainApplication.getContext(), dbname);
            result = snappydb.exists(key);
            snappydb.close();
        } catch (SnappydbException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void destroy(String dbname){
        try {
            DB snappydb = DBFactory.open(MainApplication.getContext(), dbname);
            snappydb.destroy();
        } catch (SnappydbException e) {
            e.printStackTrace();
        }
    }
}
